package com.example.xymarket.common;

import android.util.Log;


import java.io.File;
import java.util.Map;


import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class HttpCon {

    public static String JsonPost(String Url,String Json,Map<String,String>headers)
    {
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, Json);
            Request.Builder requestBuilder = new Request.Builder()
                    .url(Url)
                    .method("POST", body);
            if (headers != null) {
                // 首先组拼文本类型的参数
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    requestBuilder.addHeader(entry.getKey(), entry.getValue());
                }
            }
            requestBuilder.addHeader("Content-Type", "application/json");
            Request request = requestBuilder.build();
            Response response = client.newCall(request).execute();
            return response.body().string();
        }
        catch (Exception e)
        {
            System.out.println(e.toString());
            return null;
        }
    }


    public static String OKPost(String Url,Map<String,String>params,Map<String,String>headers,Map<String,File> files)
    {
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
            MediaType mediaType = MediaType.parse("text/plain");
            MultipartBody.Builder multipartBody = new MultipartBody.Builder().setType(MultipartBody.FORM);
            if (params != null) {
                // 首先组拼文本类型的参数
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    multipartBody.addFormDataPart(entry.getKey(), entry.getValue());
                }
            }
            else  multipartBody.addFormDataPart("", "");

            if (files!= null) {
                // 首先组拼文本类型的参数
                for (Map.Entry<String, File> entry : files.entrySet()) {
                    multipartBody.addFormDataPart(entry.getKey(), entry.getValue().getName(),RequestBody.create(MediaType.parse("application/octet-stream"),
                            entry.getValue()));
                }
            }

            RequestBody body = multipartBody.build();
            Request.Builder requestBuilder = new Request.Builder()
                    .url(Url)
                    .method("POST", body);
            if (headers != null) {
                // 首先组拼文本类型的参数
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    requestBuilder.addHeader(entry.getKey(), entry.getValue());
                }
            }
            Request request = requestBuilder.build();
            Response response = client.newCall(request).execute();
            return response.body().string();
        }
        catch (Exception e)
        {
            Log.d("e",e.toString());
            return null;
        }
    }


    public static String getUserInfoMessage() {
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
            Request request = new Request.Builder()
                    .url("https://groupon.yinjiahui.cn/api/user/get_user_info")
                    .method("GET", null)
                    .addHeader("Authorization", Info.getMyUserInfo().getToken())
                    .build();
            Response response = client.newCall(request).execute();
            return response.body().string();
        }
        catch (Exception e)
        {

        }
        return null;
    }
}
